package com.eqdd.yiqidian.ui.act

import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.eqdd.yiqidian.R
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector
import com.eqdd.yiqidian.base.BaseActivity
import com.eqdd.yiqidian.base.Config
import com.eqdd.yiqidian.box.ItemDecoration.DividerLineItemDecoration
import com.eqdd.yiqidian.helper.mvchelper.ModelRx2DataSource
import com.eqdd.yiqidian.http.HttpConfig
import com.eqdd.yiqidian.http.HttpResult
import com.eqdd.yiqidian.http.JsonConverter
import com.lzy.okgo.OkGo
import com.lzy.okrx2.adapter.FlowableBody
import com.shizhefei.mvc.MVCCoolHelper
import io.reactivex.Flowable
import kotlinx.android.synthetic.main.activity_recyclerview_refresh.*
import org.jetbrains.anko.toast

/**
 * Created by jhj on 18-10-6.
 */
class ActSignListActivity : BaseActivity() {
    override fun initBinding() {
        setContentView(R.layout.activity_recyclerview_refresh)
        initTopTitleBar(View.VISIBLE, "签到情况")
    }

    override fun initData() {
        val id = intent.getIntExtra(Config.ID, -1)
        val mvcHelper = MVCCoolHelper<List<ItemBean>>(coolRefreshView)
        if (id == -1) {
            toast("获取失败")
            finish()
            return
        }

        var pageNum = 0
        val list = arrayListOf<ItemBean>()
        recyclerView.addItemDecoration(DividerLineItemDecoration(this))
        recyclerView.layoutManager = LinearLayoutManager(this)
        val slimAdapterEx = SlimAdapterEx.create()
                .register<ItemBean>(R.layout.list_item_169, SlimInjector<ItemBean> { data, injector ->

                })
                .attachTo(recyclerView)
                .updateData(list as List<*>?) as SlimAdapterEx<ItemBean>
        val dataSource = ModelRx2DataSource<ItemBean>(ModelRx2DataSource.OnLoadSource<ItemBean> { page, register ->
            OkGo.post<HttpResult<List<ItemBean>>>(HttpConfig.BASE_URL + HttpConfig.ACTIVITY_SIGN_GET)
                    .params("page", pageNum)
                    .params("userGuid", user.guid)
                    .params("activityId", id)
                    .converter(object : JsonConverter<HttpResult<List<ItemBean>>>() {
                        override fun test() {
                            super.test()
                        }
                    })
                    .adapt(FlowableBody<HttpResult<List<ItemBean>>>())
                    .flatMap({ result ->
                        pageNum = result.page
                        Flowable.just(result.items ?: arrayListOf<ItemBean>())
                    })
        }, 10)

        mvcHelper.setDataSource(dataSource)
        mvcHelper.adapter = slimAdapterEx
        mvcHelper.refresh()


    }

    override fun setView() {
    }

    data class ItemBean(
            val Id: Int,// 2,
            val createTime: String,//": "2018-09-20 15:10:10",
            val username: String,//": "郭",
            val phone: String//": "15690783806"
    )
}